package com.cwj;

import com.cwj.generic.Orange;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: Chenwujie
 * @date: 2021-06-28 13:05
 */
public class FanxingTest {
    public static void main(String[] args) {
        // 数组的协变
        Fruit[] fruits = new Apple[10];
        fruits[0] = new Apple();
        fruits[1] = new Jonathan();

        // ? extends
        List<Fruit> fruitList = new ArrayList<>();
        List<Apple> appleList = new ArrayList<>();
        List<Jonathan> jonathanList = new ArrayList<>();

//        extendsLimit(fruitList); // 编译失败 ? extends Apple设置了上界，所以只允许Apple及其子类
        extendsLimit(appleList);
        extendsLimit(jonathanList);

        superLimit(fruitList);
        superLimit(appleList);
//        superLimit(jonathanList);// 编译失败 ? super Apple 设置了下界，所以只允许Apple及其超类

    }

    /**
     * ? extends Apple
     *
     * @param list
     */
    public static void extendsLimit(List<? extends Apple> list) {
        // get method
        Object object = list.get(0);
        Fruit fruit = list.get(0);
        Apple apple = list.get(0);
//        Jonathan jonathan = list.get(0);  // 编译失败

        // add method
//        list.add(new Object());           // 编译失败
//        list.add(new Fruit());            // 编译失败
//        list.add(new Apple());            // 编译失败
//        list.add(new Jonathan());         // 编译失败
//        ...
    }

    public static void superLimit(List<? super Apple> list){
        // get method
        Object object = list.get(0);
//        ...
//        Fruit fruit = list.get(0);        // 编译失败
//        Apple apple = list.get(0);        // 编译失败
//        Jonathan jonathan = list.get(0);  // 编译失败

        // add method
//        list.add(new Object());           // 编译失败
//        list.add(new Fruit());            // 编译失败
        list.add(new Apple());
        list.add(new Jonathan());
    }
}

class Fruit {
}

class Apple extends Fruit {
}

class Jonathan extends Apple {
}
